<?php 

function get_brands_parent($type = 0,$selid=0) {
	global $_G;
	get_cache('category');
	$html = $selstr = '';
	foreach ($_G['caches']['category'] as $key => $val) {
		if ( !empty($val['redirect']) || $val['type'] !=$type ) continue;
		if($val['pid'] >0) continue;
		if(!ISADMIN && $val['access'] >0 && $val['access'] !=$_G['users']['groupid']) continue;
		$selstr = $selid == $key ? 'selected="selected"' : '';
		$html .= '<option value="'.$key.'" '.$selstr.'>'.$val['catename'].'</option>';
	}
	
	return $html;
}

function get_brands_parent_options_bychild($id=0){
	global $_G;
	get_cache('category');
	$html = $selstr = '';
	if($id<1)return '';
	$type=$_G['caches']['category'][$id]['type'];
	$selid=$_G['caches']['category'][$id]['pid'];
	foreach ($_G['caches']['category'] as $key => $val) {
		if ( !empty($val['redirect']) || $val['type'] !=$type ) continue;
		if($val['pid'] >0) continue;
		if(!ISADMIN && $val['access'] >0 && $val['access'] !=$_G['users']['groupid']) continue;
		$selstr = $selid == $key ? 'selected="selected"' : '';
		$html .= '<option value="'.$key.'" '.$selstr.'>'.$val['catename'].'</option>';
	}
	
	return $html;
}
function get_brands_brother_options($id=0){
	global $_G;
	get_cache('category');
	$html = $selstr = '';
	if($id<1)return '';
	$type=$_G['caches']['category'][$id]['type'];
	$pid=$_G['caches']['category'][$id]['pid'];
	$selid=$id;
	foreach ($_G['caches']['category'] as $key => $val) {
		if ( !empty($val['redirect']) || $val['type'] !=$type || $val['pid']!=$pid ) continue;
		if(!ISADMIN && $val['access'] >0 && $val['access'] !=$_G['users']['groupid']) continue;
		$selstr = $selid == $key ? 'selected="selected"' : '';
		$html .= '<option value="'.$key.'" '.$selstr.'>'.$val['catename'].'</option>';
	}
	
	return $html;
}

function get_category_child($cid){
	if($cid<1)return ;
	global $_G;
	get_cache('category');
	foreach ($_G['caches']['category'] as $key => $val) {
		if ( !empty($val['redirect']) || $val['pid'] !=$cid ) continue;
		if(!ISADMIN && $val['access'] >0 && $val['access'] !=$_G['users']['groupid']) continue;
		$rs[$key]=$val;
	}
	return $rs;
}
function getcar_by_brand($cid=0,$id=0,$num=5){
	if($cid<1)return array();
	global $_G;
	if($id>0)$where = " AND id!=".intval($id);
	return $_G['db']->fetch_all("SELECT id,title,picture,views,price FROM".table('car_cars')."WHERE bid=".intval($cid).$where." AND status=1 ORDER BY views DESC LIMIT $num");
}
function getcar_by_category($cid=0,$id=0,$num=5){
	if($cid<1)return array();
	global $_G;
	if($id>0)$where = " AND id!=".intval($id);
	return $_G['db']->fetch_all("SELECT id,title,picture,views,price FROM".table('car_cars')."WHERE cid=".intval($cid).$where." AND status=1  ORDER BY views DESC LIMIT $num");
}
function get_category_by_type($type=0){
	global $_G;
	get_cache('category');
	$html = $selstr = '';
	foreach ($_G['caches']['category'] as $key => $val) {
		if ( !empty($val['redirect']) || $val['type'] !=$type ) continue;
		if($val['pid'] >0) continue;
		if(!ISADMIN && $val['access'] >0 && $val['access'] !=$_G['users']['groupid']) continue;
		$rs[$key]=$val;
	}
	
	return $rs;
}

function get_hot_brand($type=0,$num=5){
	global $_G;
	get_cache('category');
	$html = $selstr = '';
	foreach ($_G['caches']['category'] as $key => $val) {
		if ( !empty($val['redirect']) || $val['type'] !=$type ) continue;
		if(!ISADMIN && $val['access'] >0 && $val['access'] !=$_G['users']['groupid']) continue;
		if(++$i>$num)beak;
		$rs[$val['pid'] ][$key]=$val;
		if(count($rs)>=$num)break;
	}
	
	return $rs;
}

//资讯排行榜
function news_paihang($num=10){
	global $_G;
	$num=$num>0?intval($num):10;
	return $_G['db']->fetch_all("SELECT title,aid,default_image,summary FROM".table('article')." WHERE  status=1  ORDER BY views DESC limit ".$num);
			
}
function news_pingce($num=5){
	global $_G;
	//评测
	return $_G['db']->fetch_all("SELECT title,aid,default_image,summary FROM".table('article')."WHERE  status=1 AND cid=7 ORDER BY istop DESC,iscommend DESC, dateline DESC limit ".intval($num));
			
}
function news_jiqiao($num=5){
	global $_G;
	//买卖技巧
	return $_G['db']->fetch_all("SELECT title,aid,default_image,summary FROM".table('article')."WHERE  status=1 AND cid=8 ORDER BY istop DESC,iscommend DESC, dateline DESC limit ".intval($num));
			
}
//拼凑搜索地址
function searchurl($key,$value=null){
	global $_G;
	$url = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
	if(preg_match('/[&|\?]?page=\w+/i', $url) > 0) {
		$url= preg_replace('/[&|\?]?page=\w+/i', '', $url);
    }
	if(preg_match('/[&|\?]?'.$key.'=\w+/i', $url) > 0) {
		$url= preg_replace('/[&|\?]?'.$key.'=\w+/i', '', $url);
    }
    if(is_null($value)){
		return url($url);
    }else{
    	$url .= strspn('?',$url) ? (substr($url,-1,1)=='?'?'':'&' ): '?';
		return url($url.$key.'='.$value);
    }
}

//分类下数据
function get_brand_all_data($cid,$num=5){
	global $_G;
	if($bid>0)$where = " AND bid=".intval($bid);
	get_cache('category');
	$rs['cates']=$_G['caches']['category'][$cid];
	$keys=array_keys(get_category_child($cid));
	
	if(!empty($keys)) {
		$where =" AND bid IN(".implode(',', $keys).")";
		$rs['pid']=array_shift($keys);
		if($where) $rs['data'] = $_G['db']->fetch_all("SELECT * FROM".table('car_cars')."WHERE  status=1  $where ORDER BY isrecommend DESC, views desc limit ".intval($num));
	}
	return $rs;

}
//二手车最新
function get_new_cars($bid,$num=5){
	global $_G;
	if($bid>0)$where = " AND bid=".intval($bid);
	return $_G['db']->fetch_all("SELECT * FROM".table('car_cars')."WHERE  status=1  $where ORDER BY id desc limit ".intval($num));
}
//二手车热门
function get_hot_cars($bid,$num=5){
	global $_G;
	if($bid>0)$where = " AND bid=".intval($bid);
	return $_G['db']->fetch_all("SELECT * FROM".table('car_cars')."WHERE  status=1  $where ORDER BY views desc limit ".intval($num));
}
//二手车推荐
function get_recommend_cars($bid,$num=5){
	global $_G;
	if($bid>0)$where = " AND bid=".intval($bid);
	return $_G['db']->fetch_all("SELECT * FROM".table('car_cars')."WHERE  status=1  $where ORDER BY isrecommend DESC, views desc limit ".intval($num));
}
//最新求购
function get_latest_qiugou($num=5){
	global $_G;
	$rs['age']=array(1=>'1年以内',2=>'1-3年 ',3=>'3-5年',4=>'5-8年',5=>'8年以上');
	$rs['data']=$_G['db']->fetch_all("SELECT * FROM".table('car_qiugou')."WHERE status=1 $where ORDER BY id desc limit ".intval($num));
	return $rs;
}
//热门求购
function get_host_qiugou($num=5){
	global $_G;
	$rs['age']=array(1=>'1年以内',2=>'1-3年 ',3=>'3-5年',4=>'5-8年',5=>'8年以上');
	$rs['data']=$_G['db']->fetch_all("SELECT * FROM".table('car_qiugou')."WHERE status=1 $where ORDER BY views desc limit ".intval($num));
	return $rs;
}
//友链
function get_friendlink($num=10){
	global $_G;
	return $_G['db']->fetch_all("SELECT * FROM".table('nav')."WHERE visible=1 LIMIT ".intval($num));
	
}
//最新评估
function get_latest_pinggu($num=5){
	global $_G;
	return $_G['db']->fetch_all("SELECT * FROM".table('car_pinggu')."WHERE status=1 $where ORDER BY id desc limit ".intval($num));
}
//热门评估
function get_host_pinggu($num=5){
	global $_G;
	return $_G['db']->fetch_all("SELECT * FROM".table('car_pinggu')."WHERE status=1 $where ORDER BY views desc limit ".intval($num));
}